

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class CadastroVeiculo extends JPanel {

	private JButton gravar = new JButton("Gravar");
	private JButton painelBotao;
	JButton arquivo = new JButton("Arquivos");
	private JButton exit = new JButton("Exit");
	final JLabel labelImg = new JLabel();

	public CadastroVeiculo(final ArrayList<Veiculo> veiculos,
			final ConteudoTabelaVeiculos conteudoTabelaVeiculos) {

		final JPanel painel = new JPanel();
		painel.setLayout(new GridLayout(10, 5));

		painel.add(new JLabel("Modelo"));
		final JTextField campoModelo = new JTextField(15);
		painel.add(campoModelo);

		
		painel.add(new JLabel("Marcas"));
		Vector<String> nomes = new Vector<String>();
		for (Marcas m : Marcas.values()) {
			nomes.add(m.toString());
		}
		final JComboBox campoMarcas = new JComboBox(nomes);
		painel.add(campoMarcas);
		
		
		painel.add(new JLabel("Portas"));
		Vector<String> portas = new Vector<String>();
		for (Portas m : Portas.values()) {
			portas.add(m.toString());
		}
		final JComboBox campoPortas = new JComboBox(portas);
		painel.add(campoPortas);
		
		
		painel.add(new JLabel("Ano"));
		final JTextField campoAno = new JTextField(4);
		painel.add(campoAno);
		
		
		painel.add(new JLabel("Cor"));
		Vector <String> cores = new Vector<String>();
		for (Cores m : Cores.values()){
			cores.add(m.toString());
		}
		final JComboBox campoCor = new JComboBox(cores);
		painel.add(campoCor);
		
		painel.add(new JLabel("Imagem"));
		final JTextField campoImagem = new JTextField(15);
		painel.add(campoImagem);
		
		arquivo.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fileChooser = new JFileChooser();
				
				int retorno = fileChooser.showOpenDialog(null);
				
				if (retorno == JFileChooser.APPROVE_OPTION) {
					File file = fileChooser.getSelectedFile();
					
					try {
						BufferedImage myPicture = ImageIO.read(file);
						labelImg.setIcon(new ImageIcon(myPicture));
					} catch (IOException e) {
						labelImg.setText("Arquivo de imagem invalido!");
					}
					
				} else {
					labelImg.setText("Usuario cancelou a operacao");
				}
				
				add(labelImg, BorderLayout.CENTER);
				painel.setSize(500, 500);
				painel.setVisible(true);
			}
		});
		
		
	
		add(painel);
		add(gravar);
		add(exit);  
		add(arquivo);
		
		gravar.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				
				String modelo = campoModelo.getText();
				String marcas = (String) campoMarcas.getSelectedItem();
				String portas = (String) campoPortas.getSelectedItem();
				String ano = campoAno.getText();
				String cor = (String) campoCor.getSelectedItem();
				String imagem = campoImagem.getText();

				Veiculo veiculo = new Veiculo(modelo, marcas, portas, ano, cor, imagem);
				veiculos.add(veiculo);

				conteudoTabelaVeiculos.fireTableDataChanged();
				
				campoModelo.setText("");
				campoMarcas.setSelectedItem("");
				campoPortas.setSelectedItem("");
				campoAno.setText("");
				campoCor.setSelectedItem("");
				campoImagem.setText("");

				//JOptionPane.showMessageDialog(null, "Prencha todos os campos!!!");
			}
		});
		
		

		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

	}

	public JButton getPainelBotao() {
		return painelBotao;
	}

	public void setPainelBotao(JButton painelBotao) {
		this.painelBotao = painelBotao;
	}

	public JButton getExit() {
		return exit;
	}

	public void setExit(JButton exit) {
		this.exit = exit;
	}

}
